package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import view.ListView;

/**
 * 
 * @author Nelly-Kim TRAN
 * 
 * Class that displays the data concerning a contact when the corresponding button is clicked in the list of contacts
 * 
 * @param bouton
 * 				to get the text on the button clicked (firstname and lastname of the contact)
 * @param infopanel
 * 				the panel on which the information of the contact will appear
 * @param listframe
 * 				the current frame
 * 
 *
 */

public class DetailsListener implements ActionListener{
	
	
	JButton bouton;
	JPanel infopanel;
	ListView listFrame;
	
	public DetailsListener (JButton bouton, JPanel infopanel, ListView listFrame) {
		this.bouton=bouton;
		this.infopanel=infopanel;
		this.listFrame=listFrame;
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		
		//read the data from the list and display it on the panel with setInfopanel()
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";
	
	try {
		
		listFrame.getInfopanel().removeAll();
		 
		br = new BufferedReader(new FileReader("monfichier2.txt"));
		while ((line = br.readLine()) != null) {
 
		        // use comma as separator
			String[] contact = line.split(cvsSplitBy);
			String info=contact[0]+" "+contact[1];
			System.out.println(info);
			System.out.println(bouton.getText());
			
			if(bouton.getText().equals(info)) {
				System.out.println("OK!");
				listFrame.setInfopanel(new JTextField(contact[0]), new JTextField(contact[1]), new JTextField(contact[2]), new JTextField(contact[3]),
						new JTextField(contact[4]), new JTextField(contact[5]), new JTextField(contact[6]), new JTextField(contact[7]), new JButton(contact[8]), new JButton(contact[9]),
						new JButton(contact[10]), new JTextField(contact[11]), new JTextField(contact[12]));
			}			
		}
	} catch (FileNotFoundException e1) {
		e1.printStackTrace();
	} catch (IOException e2) {
		e2.printStackTrace();
	}finally {
		try {
			br.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
		}
	}
		
	}
	


